package com.itheima.reggie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /*
     *    登录
     * */
    @PostMapping("/login") // request 存取数据
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //接受密码加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据页面提交的username查询数据库
        LambdaQueryWrapper<Employee> eqw = new LambdaQueryWrapper<>();
        eqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(eqw);
        //查询是否存在
        if (emp == null) {
            //用户名不存在
            return R.error("登陆失败");
        } else {
            //判断米饭输入是否一致
            if (!emp.getPassword().equals(password)) {
                //用户密码输入
                return R.error("登陆失败");
            }
        }
        //查看员工状态 1/0
        if (emp.getStatus() == 0) {
            return R.error("员工已被禁用");
        }
        //登录成功，员工信息  id 存储至 session
        HttpSession session = request.getSession();
        //设置员工对象密码为空再存储session中
        emp.setPassword(null);
        session.setAttribute("employee", emp);
        //返回存储员工信息对象
        return R.success(emp);
    }

    /*
     *  员工退出
     * */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    /*
     * 添加员工
     * */
    @PostMapping
    public R<Employee> add(HttpSession session, @RequestBody Employee employee) {
        //设置初始密码加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
/*        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获取登录用户名
        Employee emp = (Employee) session.getAttribute("employee");
        employee.setCreateUser(emp.getId());
        employee.setUpdateUser(emp.getId());*/
        //调用
        employeeService.save(employee);
        return R.success(employee);
    }

    /*
     *   员工分页查询
     * */
    @GetMapping("/page")
    public R<Page> selectByPage(int page, int pageSize, String name) {
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Employee::getName, name);
        //设置排序时间
        lqw.orderByAsc(Employee::getUpdateTime);
        //查询
        employeeService.page(employeePage,lqw);
        return R.success(employeePage);
    }

    /*
     *  员工的禁用和启用  更新功能
     * */
    @PutMapping
    public R<String> update(HttpSession session, @RequestBody Employee employee) {
        log.info("信息：{}", employee);
        long id = Thread.currentThread().getId();
        log.info("线程id：{}",id);
        //获取修改人信息
        Employee emp = (Employee) session.getAttribute("employee");
       /* //设置更新时间
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(emp.getId());*/

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /*
     *   编辑员工
     * */
    @GetMapping("/{id}")
    public R<Employee> updateById(HttpSession session, @PathVariable Long id) {
        //获取修改人信息
        Employee emp = (Employee) session.getAttribute("employee");
        LambdaQueryWrapper<Employee> lwq = new LambdaQueryWrapper<>();
        lwq.eq(id != null, Employee::getId, id);
        Employee employee = employeeService.getOne(lwq);

        return  R.success(employee);
    }

    /*
    *   新增分类    /category
    * */
   /* @PostMapping("/category")
    public R*/

}
